﻿@using Board.Models
@{
    ViewBag.Title = "Unit";
}

<h2>Manage Units</h2>

<div class="error">@ViewBag.Message</div>

@if (ViewBag.CurrentUser != null)
{
    WebUser currentUser = (WebUser) (ViewBag.CurrentUser);
    List<SelectListItem> bedStatusDropdown = new List<SelectListItem>();
    for (int i = 0; i < Bed.BedStatusList.Count; i++)
    {
        bedStatusDropdown.Add(new SelectListItem() { Text = Bed.BedStatusDescriptionList[i], Value = Bed.BedStatusList[i] });
    }
    if (ViewBag.Bed != null)
    {
        Bed bed = (Bed) (ViewBag.Bed);
        bedStatusDropdown = new List<SelectListItem>();
        for (int i = 0; i < Bed.BedStatusList.Count; i++)
        {
            bedStatusDropdown.Add(new SelectListItem() { Text = Bed.BedStatusDescriptionList[i], Value = Bed.BedStatusList[i], Selected = Bed.BedStatusList[i].Equals(bed.BedStatus) });
        }
        <text>
            @using (Html.BeginForm("SaveBed", "Management", FormMethod.Post))
            {
                <text>
                    @Html.Hidden("BoardId", bed.BoardId)
                    <div class="display-label">
                        Site
                    </div>
                    <div class="display-field">
                        @Html.ActionLink(bed.SiteId, "Unit", new {siteId=bed.SiteId})
                    </div>
                    <div class="display-label">
                        Unit
                    </div>
                    <div class="display-field">
                        @Html.ActionLink(bed.UnitId, "Unit", new {siteId=bed.SiteId, unitId= bed.UnitId})
                    </div>
                    <div class="display-label">
                        Bed
                    </div>
                    <div class="display-field">
                        @bed.BedId 
                    </div>
                    <div class="display-label">
                        Bed Status
                    </div>
                    <div class="display-field">
                        @Html.DropDownList("BedStatus", bedStatusDropdown)
                    </div>
                    <div class="display-label">
                        Bed Type
                    </div>
                    <div class="display-field">
                        @Html.DropDownList("BedType", Bed.BedTypeList.Select(t => new SelectListItem() { Text = t, Value = t,  Selected = !string.IsNullOrEmpty(t) && bed.BedType!=null && bed.BedType.Contains(t) }), new { multiple = "multiple", @class = "multiple-select" })
                    </div>
                    <div  class="display-label">
                        <input type="submit" value="Save"/>
                    </div>
                    <div  class="display-field">
                        @Html.ActionLink("Cancel", "Unit", new{siteId=bed.SiteId, unitId=bed.UnitId})
                    </div>
                    <div class="display-label">@Html.ActionLink("Back To Complete List", "Unit")</div>
                    <div class="float-left">
                        <h4>Accessible Users:</h4>
                        @if (bed.AccessibleUsers != null && bed.AccessibleUsers.Any())
                        {
                            <text>
                                <ul>
                                    @foreach (WebUser user in bed.AccessibleUsers)
                                    {
                                        <text>
                                            <li>
                                                <div class="display-label">@user.FirstName @user.LastName</div>
                                                <div class="display-field">
                                                    @foreach (var role in user.Roles)
                                                    {
                                                        <text>
                                                            <span> @role.RoleName </span> 
                                                        </text>
                                                    }
                                                </div>
                                            </li>
                                        </text>
                                    }
                                </ul>
                            </text>
                        }
                    </div>
                </text>
            }
        </text>
    }
    else if (ViewBag.Unit != null)
    {
        Unit unit = (Unit)(ViewBag.Unit);
        if (unit.AccessibleBeds.Any())
        {
            <text>
                
                <div class="unit-div" id="UnitDiv@(unit.SiteId + "_" + unit.UnitId)">
                    <h6>@Html.ActionLink("Site " + unit.SiteId, "Unit", new{siteId=unit.SiteId}), @Html.ActionLink("Unit "+unit.UnitId, "Unit", new {siteId = unit.SiteId, unitId=unit.UnitId})</h6>
                    <div class="display-label">All beds: </div>
                    <div class="display-field">Status: @Html.DropDownList("UnitStatus"+unit.SiteId+"_"+unit.UnitId, bedStatusDropdown)  BedType:@Html.DropDownList("UnitBedType"+unit.SiteId+"_"+unit.UnitId, Bed.BedTypeList.Select(t => new SelectListItem() { Text = t, Value = t,  }), new { multiple = "multiple", @class = "multiple-select" }) <input type="button" value="Save", class="unit-save" id="UnitSave@(unit.SiteId + "_" + unit.UnitId)"/></div>
                    <div class="clear"></div>
                    @foreach (Bed bed in unit.AccessibleBeds.ToList().OrderBy(b => b.BedId))
                    {
                        <text>
                            <div class="bed-div status-@(bed.BedStatus)@(string.IsNullOrEmpty(bed.BedType)?"":" "+bed.BedType)">@Html.ActionLink(bed.BedId, "Unit", new{siteId = bed.SiteId, unitId = bed.UnitId, @bed = bed.BedId})</div>
                        </text>
                    }
                </div>
                <div class="display-label">@Html.ActionLink("Back To Complete List", "Unit")</div>
            </text>
        }
    }
    else if (ViewBag.Site != null)
    {
        Site site = (Site) (ViewBag.Site);
        <text>
            <div class="site-div" id="SiteDiv@(site.SiteId)">
                <h4>Site @site.SiteId</h4>
                @*<div class="display-label">All beds: </div>
                <div class="display-field">Status: @Html.DropDownList("SiteStatus"+site.SiteId, bedStatusDropdown)  BedType:@Html.DropDownList("SiteBedType"+site.SiteId, Bed.BedTypeList.Select(t => new SelectListItem() { Text = t, Value = t,  }), new { multiple = "multiple", @class = "multiple-select" }) <input type="button" value="Save", class="site-save" id="SiteSave@(site.SiteId)"/></div>*@
                @foreach (Unit unit in site.AccessibleUnit.ToList().OrderBy(u => u.UnitId))
                {
                    <text>
                        <div class="unit-div" id="UnitDiv@(unit.SiteId + "_" + unit.UnitId)">
                            <h6>@Html.ActionLink("Unit " + unit.UnitId, "Unit", new {siteId = unit.SiteId, unitId = unit.UnitId})</h6>
                            <div class="display-label">All beds: </div>
                    <div class="display-field">Status: @Html.DropDownList("UnitStatus"+unit.SiteId+"_"+unit.UnitId, bedStatusDropdown)  BedType:@Html.DropDownList("UnitBedType"+unit.SiteId+"_"+unit.UnitId, Bed.BedTypeList.Select(t => new SelectListItem() { Text = t, Value = t,  }), new { multiple = "multiple", @class = "multiple-select" }) <input type="button" value="Save", class="unit-save" id="UnitSave@(unit.SiteId + "_" + unit.UnitId)"/></div>
                            <div class="clear"></div>
                            @foreach (Bed bed in unit.AccessibleBeds.ToList().OrderBy(b => b.BedId))
                            {
                                <text>
                                    <div class="bed-div status-@(bed.BedStatus)@(string.IsNullOrEmpty(bed.BedType)?"":" "+bed.BedType)">@Html.ActionLink(bed.BedId, "Unit", new {siteId = bed.SiteId, unitId = bed.UnitId, @bed = bed.BedId})</div>
                                </text>
                            }
                        </div>
                    </text>
                }
            </div>
            <div class="display-label">@Html.ActionLink("Back To Complete List", "Unit")</div>
        </text>
    }
    else
    {
        <text>
            <h2>Accessible Units</h2>
        <div id="userPermissionDiv">
            
            @foreach (Site site in currentUser.AccessibleSites) { 
                <text>
                    <div class="site-div" id="SiteDiv@(site.SiteId)">
                        <h4>@Html.ActionLink("Site " + site.SiteId, "Unit", new {siteId=site.SiteId})</h4>
                        @foreach (Unit unit in site.AccessibleUnit.ToList().OrderBy(u=>u.UnitId)) { 
                            <text>
                                <div class="unit-div" id="UnitDiv@(unit.SiteId + "_" + unit.UnitId)">
                                    <h6>@Html.ActionLink("Unit "+unit.UnitId, "Unit", new {siteId = unit.SiteId, unitId=unit.UnitId})</h6>
                                    <div class="display-label">All beds: </div>
                    <div class="display-field">Status: @Html.DropDownList("UnitStatus"+unit.SiteId+"_"+unit.UnitId, bedStatusDropdown)  BedType:@Html.DropDownList("UnitBedType"+unit.SiteId+"_"+unit.UnitId, Bed.BedTypeList.Select(t => new SelectListItem() { Text = t, Value = t,  }), new { multiple = "multiple", @class = "multiple-select" }) <input type="button" value="Save", class="unit-save" id="UnitSave@(unit.SiteId + "_" + unit.UnitId)"/></div>
                                    <div class="clear"></div>
                                    @foreach (Bed bed in unit.AccessibleBeds.ToList().OrderBy(b=>b.BedId)) { 
                                        <text>
                                            <div class="bed-div status-@(bed.BedStatus)@(string.IsNullOrEmpty(bed.BedType)?"":" "+bed.BedType)">@Html.ActionLink(bed.BedId, "Unit", new{siteId = bed.SiteId, unitId = bed.UnitId, @bed = bed.BedId})</div>
                                        </text>
                                    }
                                </div>
                            </text>
                        }
                    </div>
                </text>
            }
        </div>
        </text>
    }
}

@section scripts
{
    <script src="~/Scripts/management.js"></script>
}
